/*
 * SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
 *
 * SPDX-License-Identifier: MulanPSL-2.0
 */
#![allow(
    non_camel_case_types,
    non_upper_case_globals,
    unused_assignments,
    unused_mut
)]

use crate::common::*;

/*#[macro_export]
macro_rults! sudo_sys_signame {
    () => {
        sys_sigabbrev
    };
}*/

extern "C" {
    fn __ctype_b_loc() -> *mut *const libc::c_ushort;
    fn sudo_strtonum(
        _: *const libc::c_char,
        _: libc::c_longlong,
        _: libc::c_longlong,
        _: *mut *const libc::c_char,
    ) -> libc::c_longlong;
    fn strncmp(
        __s1: *const libc::c_char,
        __s2: *const libc::c_char,
        __n: libc::c_ulong,
    ) -> libc::c_int;
    fn __libc_current_sigrtmin() -> libc::c_int;
    fn __libc_current_sigrtmax() -> libc::c_int;
    fn strcmp(_: *const libc::c_char, _: *const libc::c_char) -> libc::c_int;
    fn sysconf(__name: libc::c_int) -> libc::c_long;
    fn strcasecmp(_: *const libc::c_char, _: *const libc::c_char) -> libc::c_int;
    //static sys_sigabbrev: [*const libc::c_char; 65];
    fn sigabbrev_np(sig:i32) -> *mut *const libc::c_char;
    fn __errno_location() -> *mut libc::c_int;
}

pub struct sigalias {
    pub name: *const libc::c_char,
    pub number: libc::c_int,
}
static mut sigaliases: [sigalias; 6] = [
    {
        sigalias {
            name: b"ABRT\0" as *const u8 as *const libc::c_char,
            number: SIGABRT,
        }
        //sigabrt
    },
    {
        sigalias {
            name: b"CLD\0" as *const u8 as *const libc::c_char,
            number: SIGCLD,
        }
        //sigcld
    },
    {
        sigalias {
            name: b"IO\0" as *const u8 as *const libc::c_char,
            number: SIGIO,
        }
        //sigio
    },
    {
        sigalias {
            name: b"IOT\0" as *const u8 as *const libc::c_char,
            number: SIGIOT,
        }
        //sigiot
    },
    {
        sigalias {
            name: b"POLL\0" as *const u8 as *const libc::c_char,
            number: SIGPOLL,
        }
        //sigpoll
    },
    {
        sigalias {
            name: 0 as *const libc::c_char,
            number: -1,
        }
        //init
    },
];

//#define	SIGABRT		6	/* Abnormal termination.  */
pub const SIGABRT: libc::c_int = 6;
pub const SIGCLD: libc::c_int = 17;
pub const SIGIO: libc::c_int = 29;
pub const SIGIOT: libc::c_int = 6;
pub const SIGPOLL: libc::c_int = 29;

#[no_mangle]
pub fn sudo_str2sig(mut signame: *const libc::c_char, mut result: *mut libc::c_int) -> libc::c_int {
    let mut alias: *mut sigalias = std::ptr::null_mut::<sigalias>();
    let mut errstr: *const libc::c_char = std::ptr::null::<libc::c_char>();
    let mut signo: libc::c_int = 0;

    unsafe {
        if (*(*__ctype_b_loc()).offset(*signame.offset(0_isize) as libc::c_uchar as isize)
            as libc::c_int
            & _ISdigit as libc::c_ushort as libc::c_int)
            != 0
        {
            signo = sudo_strtonum(
                signame,
                0 as libc::c_longlong,
                (NSIG - 1) as libc::c_longlong,
                &mut errstr,
            ) as libc::c_int;
            if !errstr.is_null() {
                return -1;
            }
            *result = signo;
            return 0;
        }

        if strncmp(
            signame,
            b"RTMIN\0" as *const u8 as *const libc::c_char,
            5 as libc::c_ulong,
        ) == 0
        {
            if *signame.offset(5_isize) as libc::c_int == '\u{0}' as i32 {
                *result = SIGRTMIN!();
                return 0;
            }
            /*if *signame.offset(5_isize) as libc::c_int == '+' as i32 {
                if (*(*__ctype_b_loc())
                    .offset(*signame.offset(6_isize) as libc::c_uchar as isize)
                    as libc::c_int
                    & _ISdigit as libc::c_ushort as libc::c_int)
                    != 0
                {
                    let rtmax: libc::c_long = sysconf(_SC_RTSIG_MAX);
                    let off: libc::c_int = *signame.offset(6_isize) as libc::c_int - '0' as i32;

                    if rtmax > 0 as libc::c_long
                        && (off as libc::c_long) < (rtmax / 2 as libc::c_long)
                    {
                        *result = SIGRTMIN!() + off;
                        return 0;
                    }
                }
            }*/

            if *signame.offset(5_isize) as libc::c_int == '+' as i32
                && (*(*__ctype_b_loc()).offset(*signame.offset(6_isize) as libc::c_uchar as isize)
                    as libc::c_int
                    & _ISdigit as libc::c_ushort as libc::c_int)
                    != 0
            {
                let rtmax: libc::c_long = sysconf(_SC_RTSIG_MAX);
                let off: libc::c_int = *signame.offset(6_isize) as libc::c_int - '0' as i32;

                if rtmax > 0 as libc::c_long && (off as libc::c_long) < (rtmax / 2 as libc::c_long)
                {
                    *result = SIGRTMIN!() + off;
                    return 0;
                }
            }
        }

        if strncmp(
            signame,
            b"RTMAX\0" as *const u8 as *const libc::c_char,
            5 as libc::c_ulong,
        ) == 0
        {
            if *signame.offset(5_isize) as libc::c_int == '\u{0}' as i32 {
                *result = SIGRTMAX!();
                return 0;
            }
            /*if *signame.offset(5_isize) as libc::c_int == '-' as i32 {
                if (*(*__ctype_b_loc())
                    .offset(*signame.offset(6_isize) as libc::c_uchar as isize)
                    as libc::c_int
                    & _ISdigit as libc::c_ushort as libc::c_int)
                    != 0
                {
                    let rtmax: libc::c_long = sysconf(_SC_RTSIG_MAX);
                    let off: libc::c_int = *signame.offset(6_isize) as libc::c_int - '0' as i32;
                    if rtmax > 0 as libc::c_long
                        && (off as libc::c_long) < (rtmax / 2 as libc::c_long)
                    {
                        *result = SIGRTMAX!() - off;
                        return 0;
                    }
                }
            }*/
            if *signame.offset(5_isize) as libc::c_int == '-' as i32
                && (*(*__ctype_b_loc()).offset(*signame.offset(6_isize) as libc::c_uchar as isize)
                    as libc::c_int
                    & _ISdigit as libc::c_ushort as libc::c_int)
                    != 0
            {
                let rtmax: libc::c_long = sysconf(_SC_RTSIG_MAX);
                let off: libc::c_int = *signame.offset(6_isize) as libc::c_int - '0' as i32;
                if rtmax > 0 as libc::c_long && (off as libc::c_long) < (rtmax / 2 as libc::c_long)
                {
                    *result = SIGRTMAX!() - off;
                    return 0;
                }
            }
        }

        alias = sigaliases.as_mut_ptr();
        while !((*alias).name).is_null() {
            if strcmp(signame, (*alias).name) == 0 {
                *result = (*alias).number;
                return 0;
            }
            alias = alias.offset(1);
        }
        signo = 1;
        while signo < NSIG as libc::c_int {
            if !sigabbrev_np(signo).is_null() {
                if strcasecmp(signame, sigabbrev_np(signo) as *const libc::c_char) == 0 {
                    *result = signo;
                    return 0;
                }
            }

            if !sigabbrev_np(signo).is_null()
                && strcasecmp(signame, sigabbrev_np(signo) as *const libc::c_char) == 0
            {
                *result = signo;
                return 0;
            }
            signo += 1;
        }
        *__errno_location() = EINVAL;
    } //unsafe
    -1
}
